package com.telsoft.hms.data;

import java.sql.SQLException;

import org.slf4j.LoggerFactory;

import com.telsoft.hms.entity.ETT_AM_USER;
import com.telsoft.hms.function.HMS_Login;
import com.telsoft.hms.lib.admin.client.data.ClientDataPreprocessor;

public class DATA_Login extends ClientDataPreprocessor {
	public ETT_AM_USER login(String keyHospital, String strUser, String strPass)
			throws SQLException, Exception {
		String strSQL = "SELECT a.user_id, a.user_name, "
				+ " a.full_name, a.password,  a.status "
				+ " FROM am_user a "
				+ " WHERE UPPER(a.user_name) = ? AND a.status = 1 AND a.password = ? ";
		ETT_AM_USER ettAmUser = null;
		try {
			open(keyHospital);
			mConnection = getConnection();
			mStmt = mConnection.prepareStatement(strSQL);
			mStmt.setString(1, strUser.split("@")[0].toUpperCase());
			mStmt.setString(2, strPass);
			mRs = mStmt.executeQuery();
			if (mRs.next()) {
				ettAmUser = new ETT_AM_USER();
				ettAmUser.setUserId(mRs.getInt("user_id"));
				ettAmUser.setUserName(mRs.getString("user_name"));
				ettAmUser.setFullName(mRs.getString("full_name"));
				ettAmUser.setPassword(mRs.getString("password"));
				ettAmUser.setStatus(mRs.getInt("status"));
			} else {
				throw new Exception("ERR-0008");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			LoggerFactory.getLogger(HMS_Login.class).info(e.getMessage());
			throw new Exception(e.getMessage());
		} finally {
			close(mRs);
			close(mStmt);
			close(mConnection);
		}
		return ettAmUser;
	}
}
